
/**
 * Copyright 2017 Yonyou Corporation Ltd. All Rights Reserved.
 * This software is published under the terms of the Yonyou Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 *
 * @Project Name : dmscloud.manage
 * @File name : OrgPositionServiceImpl.java
 * @Author : yll
 * @Date : 2017年4月6日
 * <p>
 * ----------------------------------------------------------------------------------
 * Date       Who       Version     Comments
 * 1. 2017年4月6日    yll    1.0
 * <p>
 * <p>
 * <p>
 * <p>
 * ----------------------------------------------------------------------------------
 */

package com.yonyou.cyxdms.sysAuth.service.service.orgposition;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.constants.CommonConstants;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.dto.orgposition.OrgPositionPcDTO;
import com.yonyou.cyxdms.sysAuth.service.entity.*;
import com.yonyou.cyxdms.sysAuth.service.mapper.*;
import com.yonyou.cyxdms.sysAuth.service.service.role.RoleService;
import com.yonyou.cyxdms.sysAuth.service.vo.orgposition.OrgPositionDepartVO;
import com.yonyou.cyxdms.sysAuth.service.vo.orgposition.OrgPositionPageVO;
import com.yonyou.cyxdms.sysAuth.service.vo.role.RoleVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 职位实现类
 *
 * @author yll
 * @date 2017年4月6日
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrgPositionServiceImpl implements OrgPositionService {

    @Autowired
    OrgPositionMapper orgPositionMapper;

    @Autowired
    UserOrgPositionMapper userOrgPositionMapper;

    @Autowired
    PermissionMenuMapper permissionMenuMapper;

    @Autowired
    RoleService roleService;

    @Autowired
    MenuRangeMapper menuRangeMapper;

    @Autowired
    MenuActionMapper menuActionMapper;

    @Autowired
    PermissionCtrlMapper permissionCtrlMapper;

    @Autowired
    PermissionMenuActionMapper permissionMenuActionMapper;

    @Autowired
    PermissionMenuRangeMapper permissionMenuRangeMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    OrgPositionDeptMapper orgPositionDeptMapper;

    @Autowired
    OrgPositionRoleMapper orgPositionRoleMapper;

    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(OrgPositionServiceImpl.class);

    @Override
    public IPage<OrgPositionPageVO> queryorgpositiondms(Page<OrgPositionPageVO> page, Map<String, Object> queryCondition) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        queryCondition.put("appId", loginInfoDto.getAppId());
        queryCondition.put("companyId", loginInfoDto.getCompanyId());
        String dataSource = (String) queryCondition.get(CommonConstants.DATA_SOURCE);
        List<String> list = new ArrayList<>();

        logger.debug("list=============={}:", list);

        if (!StringUtils.isNullOrEmpty(dataSource)) {
            String[] ids = dataSource.split(",");
            for (String splId : ids) {
                list.add(splId);
            }
        }
        queryCondition.put(CommonConstants.DATA_SOURCE, list);

        List<OrgPositionPageVO> voList = orgPositionMapper.pageOrgPosition(page, queryCondition);
        page.setRecords(voList);
        return page;
    }

    /**
     * 根据职务id获取所挂角色
     *
     * @author dengqiang
     * @since 2019/7/11
     */
    @Override
    public List<RoleVO> getOrgPositionRoleById(Long id) {
        return orgPositionMapper.getOrgPositionRoleById(id);
    }

    /**
     * 根据职务id获取所挂组织
     *
     * @author dengqiang
     * @since 2019/7/11
     */
    @Override
    public List<OrgPositionDepartVO> getOrgPositionDepartById(Long id) {
        return orgPositionMapper.getOrgPositionDepartById(id);
    }

    /**
     * 新增职务
     *
     * @author dengqiang
     * @since 2019/7/11
     */
    @Override
    public Integer addOrgPosition(OrgPositionPcDTO ptdto) {
        if (!CommonUtils.isNullOrEmpty(getPositionByCode(ptdto))) {
            throw new ServiceBizException("已存在此职位代码");
        }
        if (!CommonUtils.isNullOrEmpty(getPositionByName(ptdto))) {
            throw new ServiceBizException("已存在此职位名称");
        }

        OrgPositionPO ptPo = new OrgPositionPO();
        setPositionPo(ptPo, ptdto);
        orgPositionMapper.insert(ptPo);

        logger.debug("ptPo==================================={}:", ptPo.getOrgPositionId());
        logger.debug("ptPo222==================================={}:", ptPo.getOrgPositionId());

        // 职位下的挂角色和组织

        for (int i = 0; i < ptdto.getDepartGmsList().size(); i++) {
            OrgPositionDeptPO orgpositiondeptpo = new OrgPositionDeptPO();
            orgpositiondeptpo.setPositionId(ptPo.getOrgPositionId());
            orgpositiondeptpo.setOrgdeptId(ptdto.getDepartGmsList().get(i).getOrganizationId());
            orgPositionDeptMapper.insert(orgpositiondeptpo);

        }

        for (int i = 0; i < ptdto.getRoleGmsSelectList().size(); i++) {
            OrgPositionRolePO orgpositionrolepo = new OrgPositionRolePO();
            orgpositionrolepo.setOrgRoleId(ptPo.getOrgPositionId());
            orgpositionrolepo.setRoleId(ptdto.getRoleGmsSelectList().get(i).getRoleId());
            orgPositionRoleMapper.insert(orgpositionrolepo);
        }

        return ptPo.getOrgPositionId();
    }

    /**
     * code查重
     *
     * @author yll
     * @date 2017年4月6日
     */
    public List<Map<String, Object>> getPositionByCode(OrgPositionPcDTO ptdto) {
        return orgPositionMapper.getPositionByCode(ptdto.getPositionCode(), ptdto.getDataSource(), ptdto.getDataType());
    }

    /**
     * name查重
     *
     * @author yll
     * @date 2017年4月6日
     */
    public List<Map<String, Object>> getPositionByName(OrgPositionPcDTO ptdto) {

        return orgPositionMapper.getPositionByName(ptdto.getPositionName(), ptdto.getDataSource(), ptdto.getDataType());
    }

    /**
     * 设置职位
     *
     * @param ptPo
     * @param ptdto
     * @author yll
     * @date 2017年4月6日
     */
    public void setPositionPo(OrgPositionPO ptPo, OrgPositionPcDTO ptdto) {

        ptPo.setPositionCode(ptdto.getPositionCode());
        ptPo.setPositionName(ptdto.getPositionName());
        ptPo.setIsValid(ptdto.getIsValid());
        ptPo.setPositionDesc(ptdto.getPositionDesc());
        ptPo.setDataSource(ptdto.getDataSource());
        ptPo.setDataType(ptdto.getDataType());

    }

    @Override
    public OrgPositionPO getOrgPositionById(Long id) {
        logger.debug("id================={}:", id);
        return orgPositionMapper.selectById(id);
    }

    /**
     * 获取职位对应的 角色id字符串
     *
     * @param id
     * @return
     * @throws ServiceBizException
     * @author yll
     * @date 2017年4月12日
     */
    @Override
    public String findRoleIdsByPositionId(Long id) {

        List<Map<String, Object>> list = orgPositionMapper.findRoleIdsByPositionId(id);

        String roleIdsStr = "";
        roleIdsStr = CommonUtils.listMapToString(list, ",");

        return roleIdsStr;

    }

    /**
     * dms创建用户时权限的操作
     * MAXINGJIAN 190718
     */
    @Override
    public void adduserpositionsperdms(Long userId, String positionIds, Integer permisionType, List<String> maintain, List<String> accessories) {
        if (StringUtils.isNullOrEmpty(positionIds)) {
            throw new ServiceBizException("用户职位不能为空");
        }
        // 先查询用户角色关系表，看查询到的用户id与传过来的用户id是否一致
        List<Map> positionList = userOrgPositionMapper.getPositionidWithUserId(userId.intValue());
        String roleIdsStr = "";
        roleIdsStr = CommonUtils.listMapToString(positionList, ",");
        String[] positionIdsStrList = roleIdsStr.split(",");
        String[] idList = positionIds.split(",");
        boolean isequal = equalstwoString(positionIdsStrList, idList);
        if (!isequal) {
            // 如果两个id串不相等且用户之前下面曾经有职位，那么把之前的权限都删了或者置空
            if (!CommonUtils.isNullOrEmpty(positionList)) {
                // 如果之前就挂了职位，那么把权限置空
                this.emptyPositionById(userId, permisionType);
            }
            userOrgPositionMapper.deleteByuserId(userId.intValue());
            // 把职位对应的职位存到用户职位关系表中
            for (String anIdList : idList) {
                UserOrgPositionPO orgpositionrolepo = new UserOrgPositionPO();
                orgpositionrolepo.setUserId(userId.intValue());
                orgpositionrolepo.setOrgPositionId(Integer.parseInt(anIdList));
                userOrgPositionMapper.insert(orgpositionrolepo);
            }
            /// --------------------------------------------------
            if (!CommonUtils.isNullOrEmpty(positionList)) {// 如果之前就挂了职位，那么把权限置空,然后修改对应权限
                //通过userid获取用户权限
                Map map = userOrgPositionMapper.queryRoleIds(userId.intValue());
                String ids = map.get("roleIds").toString();
                String[] idsArray = null;
                if (!StringUtils.isNullOrEmpty(ids)) {
                    idsArray = ids.split(",");
                }
                Map<String, Object> params = new HashMap<>();
                params.put("idsArray", idsArray);
                List<Map> orgList = permissionMenuMapper.queryPermissionMenu(ManageDictCodeConstants.DATA_TYPE_BY_OWNER, ManageDictCodeConstants.BILL_TYPE_PERMISSION_ROLE, params);
                if (!CommonUtils.isNullOrEmpty(orgList)) {
                    for (int i = 0; i < orgList.size(); i++) {
                        List<PermissionMenuPO> recordpolist = getPermissionMenu(permisionType, userId.intValue(), i, orgList);
                        if (recordpolist.get(0) != null) {
                            PermissionMenuPO permissionmenupo = recordpolist.get(0);
                            permissionmenupo.setIsFlexible(Integer.parseInt(orgList.get(i).get(CommonConstants.IS_FLEXIBLE_2).toString()));
                            permissionmenupo.setIsEmpowered(Integer.parseInt(orgList.get(i).get(CommonConstants.IS_EMPOWERED_2).toString()));
                            permissionMenuMapper.updateById(permissionmenupo);
                        }
                    }
                }
                /// 合并角色菜单操作权限
                // 先查到tc_menu_action中设置好的固定的权限???
                List<Map> actionresult = menuActionMapper.getMenuAction();

                doActionresult(actionresult, params);

                if (!CommonUtils.isNullOrEmpty(actionresult)) {
                    for (int k = 0; k < actionresult.size(); k++) {
                        List<PermissionMenuPO> recordpolist = getPermissionMenu(permisionType, userId.intValue(), k, actionresult);
                        if (recordpolist.size() != 0 && recordpolist.get(0) != null) {
                            Map<String, Object> param = new HashMap<>();
                            param.put(CommonConstants.IS_FLEXIBLE_2, actionresult.get(k).get(CommonConstants.IS_FLEXIBLE_2));
                            param.put(CommonConstants.IS_EMPOWERED_2, actionresult.get(k).get(CommonConstants.IS_EMPOWERED_2));
                            param.put("menuCuringId", actionresult.get(k).get("menuCuringId"));
                            param.put("permissionMenuId", recordpolist.get(0).getPermissionMenuId());
                            permissionMenuActionMapper.modifyPermissionMenuAction2(param);
                        }

                    }
                }
                /// 合并角色菜单数据范围权限
                List<Map> rangeresult = menuRangeMapper.getMenuRange();
                doRangeresult(rangeresult, params);
                if (!CommonUtils.isNullOrEmpty(rangeresult)) {
                    for (int z = 0; z < rangeresult.size(); z++) {
                        List<PermissionMenuPO> recordpolist = getPermissionMenu(permisionType, userId.intValue(), z, rangeresult);
                        if (recordpolist.size() != 0 && recordpolist.get(0) != null) {
                            Map<String, Object> param = new HashMap<>();
                            param.put(CommonConstants.IS_FLEXIBLE_2, actionresult.get(z).get(CommonConstants.IS_FLEXIBLE_2));
                            param.put(CommonConstants.IS_EMPOWERED_2, actionresult.get(z).get(CommonConstants.IS_EMPOWERED_2));
                            param.put("menuRangeId", actionresult.get(z).get("menuRangeId"));
                            param.put("permissionMenuId", recordpolist.get(0).getPermissionMenuId());
                            permissionMenuRangeMapper.modifyPermissionMenuRange2(param);
                        }
                    }
                }
                // }

            } else {// 如果之前没挂，则新增-----------------------------------------------------------------------------------------------
                // if(StringUtils.isEquals(ManageDictCodeConstants.STATUS_IS_YES, isFixed.toString())){//如果是固化的
                /// 存合并角色后的菜单权限
//                 Map map = userOrgPositionMapper.queryRoleIds(userId.intValue());
//                 String ids = map.get("roleIds").toString();
//                 String[] idsArray = null;
//                 if (!StringUtils.isNullOrEmpty(ids)) {
//                     idsArray = ids.split(",");
//                 }
                Map<String, Object> params = new HashMap<>();
                params.put("idsArray", null);
                List<Map> orgList = permissionMenuMapper.queryPermissionMenu(ManageDictCodeConstants.DATA_TYPE_BY_OWNER, ManageDictCodeConstants.BILL_TYPE_PERMISSION_ROLE, params);
                if (!CommonUtils.isNullOrEmpty(orgList)) {
                    for (int i = 0; i < orgList.size(); i++) {
                        PermissionMenuPO permissionmenupo = new PermissionMenuPO();
                        permissionmenupo.setPermissionType(permisionType);
                        permissionmenupo.setPermissionId(userId.intValue());
                        permissionmenupo.setMenuId(Long.parseLong(orgList.get(i).get("menuId").toString()));
                        permissionmenupo.setIsEmpowered(Integer.parseInt(orgList.get(i).get(CommonConstants.IS_FLEXIBLE_2).toString()));
                        permissionmenupo.setIsFlexible(Integer.parseInt(orgList.get(i).get(CommonConstants.IS_EMPOWERED_2).toString()));
                        permissionMenuMapper.insert(permissionmenupo);
                    }
                }

                List<Map> actionresult = menuActionMapper.getMenuAction();

                doActionresult(actionresult, params);
                if (!CommonUtils.isNullOrEmpty(actionresult)) {
                    for (int k = 0; k < actionresult.size(); k++) {
                        List<PermissionMenuPO> recordpolist = getPermissionMenu(permisionType, userId.intValue(), k, actionresult);
                        if (recordpolist.size() != 0 && recordpolist.get(0) != null) {
                            PermissionMenuActionPO permissionmenuactionpo = new PermissionMenuActionPO();
                            permissionmenuactionpo.setMenuCuringId(Integer.parseInt(actionresult.get(k).get("menuCuringId").toString()));
                            permissionmenuactionpo.setPermissionMenuId(recordpolist.get(0).getPermissionMenuId());
                            permissionmenuactionpo.setIsEmpowered(Integer.parseInt(actionresult.get(k).get(CommonConstants.IS_EMPOWERED_2).toString()));
                            permissionmenuactionpo.setIsFlexible(Integer.parseInt(actionresult.get(k).get(CommonConstants.IS_FLEXIBLE_2).toString()));
                            permissionMenuActionMapper.insert(permissionmenuactionpo);
                        }
                    }
                }

                /// 合并角色菜单数据范围权限
                List<Map> rangeresult = menuRangeMapper.getMenuRange();
                doRangeresult(rangeresult, params);
                if (!CommonUtils.isNullOrEmpty(rangeresult)) {
                    // 非空则进行循环
                    for (int z = 0; z < rangeresult.size(); z++) {
                        List<PermissionMenuPO> recordpolist = getPermissionMenu(permisionType, userId.intValue(), z, rangeresult);
                        if (recordpolist.size() != 0 && recordpolist.get(0) != null) {
                            PermissionMenuRangePO permissionmenurangepo = new PermissionMenuRangePO();
                            permissionmenurangepo.setMenuRangeId(Integer.parseInt(rangeresult.get(z).get("menuRangeId").toString()));
                            permissionmenurangepo.setPermissionMenuId(recordpolist.get(0).getPermissionMenuId());
                            permissionmenurangepo.setIsEmpowered(Integer.parseInt(rangeresult.get(z).get(CommonConstants.IS_EMPOWERED_2).toString()));
                            permissionmenurangepo.setIsFlexible(Integer.parseInt(rangeresult.get(z).get(CommonConstants.IS_FLEXIBLE_2).toString()));
                            permissionMenuRangeMapper.insert(permissionmenurangepo);
                        }

                    }
                }
            }
        }

    }

    public List<PermissionMenuPO> getPermissionMenu(Integer permisionType, Integer userId, int n, List<Map> list) {
        LambdaQueryWrapper<PermissionMenuPO> lambdaQueryWrapper = new QueryWrapper().lambda();
        lambdaQueryWrapper.eq(PermissionMenuPO::getPermissionType, permisionType);
        lambdaQueryWrapper.eq(PermissionMenuPO::getPermissionId, userId);
        String menuId = list.get(n).get("menuId").toString();
        lambdaQueryWrapper.eq(PermissionMenuPO::getMenuId, Integer.parseInt(menuId));
        return permissionMenuMapper.selectList(lambdaQueryWrapper);
    }

    // 在不考虑hash碰撞的情况，使用String的hashcode，来进行所有值的异或结果可以验证是否相等
    public static boolean equalstwoString(String[] a, String[] b) {
        if (a.length != b.length) {
            return false;
        }
        int n = a[0].hashCode() ^ b[0].hashCode();
        for (int i = 1; i < a.length; i++) {
            n ^= a[i].hashCode() ^ b[i].hashCode();
        }
        if (n == 0) {
            return true;
        }
        return false;
    }

    /**
     * 置空职位权限
     */
    @Override
    public void emptyPositionById(Long positionId, Integer permissionType) throws ServiceBizException {
        //菜单权限置空
        List<Map<String, Object>> list = roleMapper.selectPermissionMenuId(positionId, permissionType);
        for (int i = 0; i < list.size(); i++) {
            Object menuid = list.get(i).get("PERMISSION_MENU_ID");
            Integer permissionMenuId = Integer.parseInt(menuid.toString());
            permissionMenuActionMapper.updatePermissionMenuAction(ManageDictCodeConstants.STATUS_IS_NOT, permissionMenuId);
            permissionMenuRangeMapper.updatePermissionMenuRange(ManageDictCodeConstants.STATUS_IS_NOT, permissionMenuId);
        }
        permissionMenuMapper.updatePermissionMenu(ManageDictCodeConstants.STATUS_IS_NOT, positionId, permissionType);
        //受控权限置空
        List<Map<String, Object>> list2 = roleMapper.selectPermissionCtrlId(positionId, permissionType);
        for (int i = 0; i < list2.size(); i++) {
            Object ctrlId = list2.get(i).get("PERMISSION_CTRL_ID");
            PermissionCtrlPO permissionctrlpo = permissionCtrlMapper.selectById(Integer.parseInt(ctrlId.toString()));
            permissionctrlpo.setIsEmpowered(ManageDictCodeConstants.STATUS_IS_NOT);
            permissionCtrlMapper.updateById(permissionctrlpo);
        }
    }

    /**
     * @author ：quankk
     * @date ：2019/7/24 20:37
     * @description：
     */
    @Override
    public IPage<OrgPositionPageVO> getorgPositionType(Page<OrgPositionPageVO> page, Map<String, Object> param) {
        String[] dataSourceArray = null;
        if (!StringUtils.isNullOrEmpty(param.get(CommonConstants.DATA_SOURCE))) {
            dataSourceArray = String.valueOf(param.get(CommonConstants.DATA_SOURCE)).split(",");
        }
        param.put("dataSourceArray", dataSourceArray);
        List<OrgPositionPageVO> result = orgPositionMapper.getorgPositionType(page, param);
        page.setRecords(result);
        return page;
    }

    /**
     * 根据id修改职位信息
     *
     * @param id
     * @param ptdto
     * @return void
     * @author dengqiang
     * @since 2019/8/5
     */
    @Override
    public void modifyOrgPosition(Long id, OrgPositionPcDTO ptdto) {
        OrgPositionPO ptPo = orgPositionMapper.selectById(id);
        ptPo.setPositionName(ptdto.getPositionName());
        orgPositionMapper.updateById(ptPo);
        LambdaQueryWrapper<OrgPositionDeptPO> orgPositionDeptqueryWrapper = new QueryWrapper().lambda();
        orgPositionDeptqueryWrapper.eq(OrgPositionDeptPO::getPositionId, id);
        orgPositionDeptMapper.delete(orgPositionDeptqueryWrapper);
        for (int i = 0; i < ptdto.getDepartGmsList().size(); i++) {
            OrgPositionDeptPO orgpositiondeptpo = new OrgPositionDeptPO();
            orgpositiondeptpo.setPositionId(ptPo.getOrgPositionId());
            orgpositiondeptpo.setOrgdeptId(ptdto.getDepartGmsList().get(i).getOrganizationId());
            orgPositionDeptMapper.insert(orgpositiondeptpo);

        }
        LambdaQueryWrapper<OrgPositionRolePO> orgPositionRolequeryWrapper = new QueryWrapper().lambda();
        orgPositionRolequeryWrapper.eq(OrgPositionRolePO::getOrgRoleId, id);
        orgPositionRoleMapper.delete(orgPositionRolequeryWrapper);
        for (int i = 0; i < ptdto.getRoleGmsSelectList().size(); i++) {
            OrgPositionRolePO orgpositionrolepo = new OrgPositionRolePO();
            orgpositionrolepo.setOrgRoleId(ptPo.getOrgPositionId());
            orgpositionrolepo.setRoleId(ptdto.getRoleGmsSelectList().get(i).getRoleId());
            orgPositionRoleMapper.insert(orgpositionrolepo);
        }
    }

    /**
     * @param actionresult
     * @param params
     * @return void
     * @author dengqiang
     * @since 2019/8/2
     */
    public void doActionresult(List<Map> actionresult, Map<String, Object> params) {
        if (!CommonUtils.isNullOrEmpty(actionresult)) {
            for (int i = 0; i < actionresult.size(); i++) {
                Map actionresultMap = actionresult.get(i);
                Map actionmap = new HashMap();
                actionmap.put(CommonConstants.IS_FLEXIBLE_2, 10041002);
                actionmap.put(CommonConstants.IS_EMPOWERED_2, 10041002);
                Integer menuCuringId = (Integer) actionresultMap.get("menuCuringId");
                List<Map> actionlistmap = menuActionMapper.findMenuAction(menuCuringId, params);
                if (!CommonUtils.isNullOrEmpty(actionlistmap)) {
                    List<Map> actionlistmap3 = menuActionMapper.findMenuAction2(menuCuringId, params);
                    doListmap(actionlistmap, actionlistmap3, actionmap);
                }
                actionresultMap.put(CommonConstants.IS_FLEXIBLE_2, actionmap.get(CommonConstants.IS_FLEXIBLE_2));
                actionresultMap.put(CommonConstants.IS_EMPOWERED_2, actionmap.get(CommonConstants.IS_EMPOWERED_2));
                if (StringUtils.isEquals(actionmap.get(CommonConstants.IS_FLEXIBLE_2).toString(), CommonConstants.CONSTANT_2)) {
                    actionresultMap.put(CommonConstants.IS_DISABLED_2, 10041001);
                } else {
                    actionresultMap.put(CommonConstants.IS_DISABLED_2, 10041002);
                    actionresultMap.put("actionName", actionresultMap.get("actionName") + "[可分配]");
                }

            }
        }
    }

    /**
     * @param rangeresult
     * @param params
     * @return void
     * @author dengqiang
     * @since 2019/8/2
     */
    public void doRangeresult(List<Map> rangeresult, Map<String, Object> params) {
        if (!CommonUtils.isNullOrEmpty(rangeresult)) {
            for (int i = 0; i < rangeresult.size(); i++) {
                Map rangeresultMap = rangeresult.get(i);
                Map rangemap = new HashMap();
                rangemap.put(CommonConstants.IS_FLEXIBLE_2, 10041002);
                rangemap.put(CommonConstants.IS_EMPOWERED_2, 10041002);
                rangemap.put(CommonConstants.IS_DISABLED_2, 10041002);
                Integer rangemenuRangeId = (Integer) rangeresultMap.get("menuRangeId");
                List<Map> rangelistmap = menuRangeMapper.findMenuRange(rangemenuRangeId, params);
                if (!CommonUtils.isNullOrEmpty(rangelistmap)) {
                    List<Map> rangelistmap3 = menuRangeMapper.findMenuRange2(rangemenuRangeId, params);
                    doListmap(rangelistmap, rangelistmap3, rangemap);

                }
                rangeresultMap.put(CommonConstants.IS_FLEXIBLE_2, rangemap.get(CommonConstants.IS_FLEXIBLE_2));
                rangeresultMap.put(CommonConstants.IS_EMPOWERED_2, rangemap.get(CommonConstants.IS_EMPOWERED_2));

                if (StringUtils.isEquals(rangemap.get(CommonConstants.IS_FLEXIBLE_2).toString(), CommonConstants.CONSTANT_2)) {
                    rangeresultMap.put(CommonConstants.IS_DISABLED_2, 10041001);
                } else {
                    rangeresultMap.put(CommonConstants.IS_DISABLED_2, 10041002);
                    rangeresultMap.put("codeCnDesc", rangeresultMap.get("codeCnDesc") + "[可分配]");
                }
            }
        }
    }

    /**
     * @author dengqiang
     * @since 2019/8/2
     */
    public void doListmap(List<Map> listmap, List<Map> listmap3, Map map) {
        if (!CommonUtils.isNullOrEmpty(listmap3)) {
            map.put(CommonConstants.IS_FLEXIBLE_2, 10041002);
            map.put(CommonConstants.IS_EMPOWERED_2, 10041001);
        } else {
            doMap(listmap, map);
        }
    }

    /**
     * @return void
     * @author dengqiang
     * @since 2019/8/2
     */
    public void doMap(List<Map> listmap, Map map) {
        for (int j = 0; j < listmap.size(); j++) {
            if (StringUtils.isEquals(listmap.get(j).get(CommonConstants.IS_FLEXIBLE_2) == null ? CommonConstants.CONSTANT_2 : listmap.get(j).get(CommonConstants.IS_FLEXIBLE_2).toString(),
                    CommonConstants.CONSTANT_1)) {
                map.put(CommonConstants.IS_FLEXIBLE_2, 10041001);
            }
            if (StringUtils.isEquals(listmap.get(j).get(CommonConstants.IS_EMPOWERED_2) == null ? CommonConstants.CONSTANT_2 : listmap.get(j).get(CommonConstants.IS_EMPOWERED_2).toString(),
                    CommonConstants.CONSTANT_1)) {
                map.put(CommonConstants.IS_EMPOWERED_2, 10041001);
            }
        }
    }

}
